/**
 * Created by yangjiao on 2020/3/19.
 */
import Agent from '@/components/public-modules/cache/Agent.vue'
import TimeLine from '@/components/ui-modules/timeLine/TimeLine.vue'
import {timeFormat} from '@utils/m7Utils'
import {getNotifyDate} from '@/utils/customerUtils.js'
export default {
  data () {
    return {
      isLoadMoreShow1: false,
      agents: {},
      page: 1,
      limit: 20,
      total: 0,
      currId: '',
      showLog: false,
      planningListNew: []
    }
  },
  components: {
    Agent,
    TimeLine
  },
  created () {
    let p2 = this.$store.dispatch('getCache', {type: 'agents'})
    Promise.all([p2]).then(([res2]) => {
      let myMap = {}
      for (var key in res2) {
        myMap[res2[key]._id] = res2[key]
      }
      this.agents = myMap
    })
  },
  props: {
    cid: String,
    linkPlan: {
      type: Array,
      default: () => {
        return []
      }
    },
    isLoadMoreShow: Boolean
  },
  watch: {
    cid (val) {
      this.showLog = false
    }
  },
  methods: {
    close () {
      this.showLog = false
    },
    showLogDetail1 (id) {
      this.currId = id
      this.showLog = true
      this.planningListNew = []
      this.page = 1
      this.total = 0
      this.showLogDetail()
    },
    showLogDetail () {
      let data = {
        noteId: this.currId,
        page: this.page,
        limit: this.limit
      }
      this.$store.dispatch('getCustomerNoteHistoryPlan', data).then(res => {
        if (res.success) {
          let arr = []
          let _data = res.list
          for (let i = 0; i < _data.length; i++) {
            let item = _data[i]
            let obj = {}
            if (item.status === '0') {
              obj.content = this.$t('customer.createPlan', {_action: this.agents[item.lastOperator] ? this.agents[item.lastOperator].displayName : '--'})
            } else if (item.status === '2') {
              obj.content = this.$t('customer.editPlan', {_action: this.agents[item.lastOperator] ? this.agents[item.lastOperator].displayName : '--'})
              if (item.remark) {
                obj.content += this.$t('customer.editRemark', {lastRemark: item.lastRemark, remark: item.remark})
              }
              if (item.notifyTime) {
                obj.content += this.$t('customer.editnotifyTime', {lastNotifyTime: item.lastNotifyTime, notifyTime: item.notifyTime})
              }
              if (item.executor || item.lastExecutor) {
                obj.content += this.$t('customer.editExecutor', {lastExecutor: this.agents[item.lastExecutor] ? this.agents[item.lastExecutor].displayName : '--', executor: this.agents[item.executor] ? this.agents[item.executor].displayName : '--'})
              }
            } else if (item.status === '1') {
              obj.content = this.$t('customer.finishPlan', {_action: this.agents[item.lastOperator] ? this.agents[item.lastOperator].displayName : '--'})
            } else if (item.status === '99') {
              obj.content = this.$t('customer.channelPlan', {_action: this.agents[item.lastOperator] ? this.agents[item.lastOperator].displayName : '--'})
            } else if (item.status === '9') {
              obj.content = this.$t('customer.rePlan', {_action: this.agents[item.lastOperator] ? this.agents[item.lastOperator].displayName : '--'})
            }
            obj.time = timeFormat(item.lastUpdateTime, '')
            obj.remark = item.action
            obj.customer = item.name
            obj._id = item._id
            obj.result = item.lastUpdateTime ? getNotifyDate(item.lastUpdateTime) : ''
            arr.push(obj)
          }
          if (res.count < this.limit || this.page === 1) {
            this.planningListNew = arr
          } else {
            this.planningListNew = [...this.planningListNew, ...arr]
          }
          this.total = res.count
          if (this.total > this.limit) {
            if (res.list.length === 0 || res.list.length < this.limit) {
              this.isLoadMoreShow1 = false
            } else {
              this.isLoadMoreShow1 = true
            }
          } else {
            this.isLoadMoreShow1 = false
          }
        }
      })
    },
    loadMore1 () {
      this.page++
      this.showLogDetail()
    },
    timeFormat1 (date) {
      return timeFormat(date, '')
    },
    loadMore () {
      this.$emit('load-more')
    }
  }
}

